package com.alibaba.org.service.impl;

import com.alibaba.basic.service.impl.BaseServiceImpl;
import com.alibaba.basic.util.Md5Utils;
import com.alibaba.basic.util.StrUtils;
import com.alibaba.org.domain.Employee;
import com.alibaba.org.mapper.EmployeeMapper;
import com.alibaba.org.service.IEmployeeService;
import com.alibaba.user.domain.Logininfo;
import com.alibaba.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：员工
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;


    @Transactional
    @Override
    public void add(Employee employee) {
        //  修改盐值和加密
        String salt = StrUtils.getComplexRandomString(32);
        String md5 = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5);

        // 先添加loginfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        logininfoMapper.add(logininfo);

        // 在添加到user
        employee.setLogininfoId(logininfo.getId());
        super.add(employee);


        employeeMapper.addEmpolee(employee);


    }

    @Transactional
    @Override
    public void update(Employee employee) {
        //  修改盐值和加密
        String salt = StrUtils.getComplexRandomString(32);
        String md5 = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5);

        Employee oldEmployee = employeeMapper.findById(employee.getId());

        // 修改employee信息
        employee.setLogininfoId(oldEmployee.getLogininfoId());
        employee.setShopId(oldEmployee.getShopId());
        super.update(employee);
        // 修改loginfo信息
        Logininfo oldLoginfo = logininfoMapper.findById(oldEmployee.getLogininfoId());
        BeanUtils.copyProperties(employee, oldLoginfo);
        oldLoginfo.setId(oldEmployee.getLogininfoId());
        logininfoMapper.update(oldLoginfo);

        // 先删除在添加
        employeeMapper.deleteEmployeeRole(employee.getId());
        employeeMapper.addEmpolee(employee);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        Employee employee = employeeMapper.findById(id);
        super.delete(id);

        // 删除loginfoid
        logininfoMapper.delete(employee.getLogininfoId());

        employeeMapper.deleteEmployeeRole(id);
    }

    @Override
    public void batchDelete(List<Long> ids) {

        for (Long id : ids) {
            delete(id);
        }
    }

    @Override
    public List<Employee> list(Long id) {
        return employeeMapper.list(id);
    }
}
